<template>
  <div class="main-wrap">
    <div class="operation-wrap">
      <el-button type="primary" size="mini" @click="openDia()">新增</el-button>
    </div>
    <el-table border :data="listData" :row-style="{ height: '40px' }" style="width: 100%">
      <el-table-column label="角色名" width="180">
        <template slot-scope="scope">
          <div>{{ scope.row.name }}</div>
        </template>
      </el-table-column>
      <el-table-column label="角色描述" width="180">
        <template slot-scope="scope">
          <div>{{ scope.row.description }}</div>
        </template>
      </el-table-column>
      <el-table-column label="角色功能">
        <template slot-scope="scope">
          <div>{{ scope.row.product }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div style="display: flex; align-items: center">
            <el-button type="text" size="mini" @click="openDia(scope.row)"> 修改 </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="fromData.id ? '修改' : '新增'" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="fromData"
        status-icon
        :rules="rules"
        ref="dataForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="fromData.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="fromData.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色功能" prop="product">
          <el-input v-model="fromData.product"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="submitForm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/req'
import { nextTick } from 'vue'
export default {
  data() {
    return {
      listData: [],
      fromData: {
        id: '',
        name: '',
        description: '',
        product: '',
      },
      rules: {},
      dialogVisible: false,
    }
  },
  mounted() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const res = await request.get('/role/getRoleList')
      this.listData = res
    },
    openDia(data) {
      if (data) {
        Object.assign(this.fromData, data)
      }
      this.dialogVisible = true
      nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
    },
    cancel() {
      this.dialogVisible = false
    },
    async submitForm() {
      if (this.fromData.id) {
        await request.post('/role/updRole', this.fromData)
      } else {
        await request.post('/role/addRole', this.fromData)
      }

      this.$message({
        message: this.fromData.id ? '编辑成功' : '添加成功',
        type: 'success',
      })
      this.dialogVisible = false
      this.getRoleList()
    },
  },
}
</script>

<style scoped></style>
